package cn.exrick.xboot.base.serviceimpl

import cn.exrick.xboot.base.entity.SecurityAdvisorExamContent
import cn.exrick.xboot.base.mapper.SecurityAdvisorExamContentMapper
import cn.exrick.xboot.base.service.ISecurityAdvisorExamContentService
import cn.exrick.xboot.base.vo.SecurityAdvisorExamContentVo
import cn.exrick.xboot.core.common.utils.PageUtil
import cn.exrick.xboot.core.common.utils.ResultUtil
import cn.exrick.xboot.core.common.utils.ToolUtil
import cn.exrick.xboot.core.common.vo.PageVo
import cn.exrick.xboot.core.common.vo.Result
import com.baomidou.mybatisplus.extension.kotlin.KtQueryWrapper
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl
import com.google.common.collect.Maps
import lombok.extern.slf4j.Slf4j
import org.springframework.stereotype.Service
import org.springframework.transaction.annotation.Transactional

/**
 * 安全顾问考试内容上传接口实现
 * @author Fei
 */
@Slf4j
@Service
@Transactional
open class ISecurityAdvisorExamContentServiceImpl : ServiceImpl<SecurityAdvisorExamContentMapper?, SecurityAdvisorExamContent?>(), ISecurityAdvisorExamContentService {


    override fun createOrUpdateExamContent(securityAdvisorExamContent: SecurityAdvisorExamContent): Result<Any>? {

        return try {

            saveOrUpdate(securityAdvisorExamContent)
            ResultUtil.success<Any>("操作成功！")
        }catch (e: Exception) {

            ResultUtil.error<Any>(e.message)
        }
    }

    override fun removeExamContent(ids: Array<out String>): Result<Any>? {

        return try {

            removeByIds(ids.toList())
            ResultUtil.success("操作成功")
        }catch (e: Exception) {

            ResultUtil.error(201, e.message)
        }
    }

    override fun getExamContentVo(id: String): SecurityAdvisorExamContentVo? {

        return getById(id)?.run {

            SecurityAdvisorExamContentVo().apply {

                ToolUtil.copyProperties(this@run, this)
            }
        }
    }

    override fun getExamContentList(pageVo: PageVo, trainingJobPartId: String): Result<MutableMap<String, Any?>>? {

        return ToolUtil.setListToNul(list(KtQueryWrapper(SecurityAdvisorExamContent::class.java).eq(SecurityAdvisorExamContent::jobTrainPartId, trainingJobPartId)
                .orderByDesc(SecurityAdvisorExamContent::createTime)))?.run {

            val result: MutableMap<String, Any?> = Maps.newHashMap()
            result["size"] = size
            result["content"] = PageUtil.listToPage(pageVo, mapNotNull { getExamContentVo(it?.id!!) }.toMutableList())

            ResultUtil.data(result, "获取数据成功")
        } ?: ResultUtil.error(201,"暂无数据")
    }

}